package com.shortvedio.media.api;


import com.shortvedio.base.context.BaseContext;
import com.shortvedio.media.service.MediaService;
import com.shortvedio.base.model.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@Api("头像更换接口")
@Slf4j
public class MediaHeadController {

    @Autowired
    MediaService mediaService;

    @PostMapping("upload_picture")//上传头像接口
    public Result<String> uploadPicture(MultipartFile multipartFile) throws IOException {
        Long userId = BaseContext.getCurrentId();
        //
        log.info("userId:{}",userId);
        //获取文件后缀
        String originalFilename = multipartFile.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        //使用uuid为文件命名   创建临时文件
        File minio = File.createTempFile(UUID.randomUUID().toString(), extension);
        multipartFile.transferTo(minio);
        //获取缓存中的路径
        String path = minio.getAbsolutePath();
        String headSrc = mediaService.uploadPicture(path);
        mediaService.updateUserHeadSrc(headSrc,userId);
        return Result.success(headSrc);
    }



}
